package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.i18n.I18nConstants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

public class ModuleSettingsView extends Composite implements ModuleSettingsPresenter.Display {

	private Presenter presenter;
	
	private static final int COLUMNS_NUMBER = 2;
	private static final int COLUMN_FIRST = 0;
	private static final int COLUMN_SECOND = 1;
	
	private final Button buttonSave = new Button();
	private final Button buttonCancel = new Button();
	private final Label title = new Label();
	private final FlexTable flexTable = new FlexTable();
	
	private final Label lLanguage = new Label();
	private final Label lPageSize = new Label();
	
	protected I18nConstants constants = GWT.create(I18nConstants.class);
	
	public ModuleSettingsView() {
		super();
		
		title.setText(constants.HeaderLinkAndLabelSettings());
		
		lLanguage.setText(constants.SettingsLabelLanguageL0());
		lPageSize.setText(constants.SettingsLabelPageSizeL0());
		
		buttonSave.setText(constants.SettingsButtonSave());
		buttonCancel.setText(constants.SettingsButtonCancel());
		
		title.setStylePrimaryName("Settings_title");
		
		flexTable.setWidth("100%");
		flexTable.setStylePrimaryName("Settings_table");
		
		FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
		
		cellFormatter.setColSpan(2, 0, COLUMNS_NUMBER);
		cellFormatter.setAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		
		
		
		FlowPanel buttons = new FlowPanel();
		buttons.setStylePrimaryName("Settings_pButtons");
		
		buttons.add(buttonSave);
		buttons.add(buttonCancel);
		
		flexTable.setWidget(0, COLUMN_FIRST, lLanguage);
		flexTable.setWidget(0, COLUMN_SECOND, getColSecondLanguage());
		
		flexTable.setWidget(1, COLUMN_FIRST, lPageSize);
		flexTable.setWidget(1, COLUMN_SECOND, getColSecondPageSize());
		
		flexTable.setWidget(2, COLUMN_FIRST, buttons);
		
		
		
		flexTable.getCellFormatter().setStylePrimaryName(0, COLUMN_FIRST, "Settings_col0");
		flexTable.getCellFormatter().setStylePrimaryName(0, COLUMN_SECOND, "Settings_col1");
		
		flexTable.getCellFormatter().setStylePrimaryName(1, COLUMN_FIRST, "Settings_col0");
		flexTable.getCellFormatter().setStylePrimaryName(1, COLUMN_SECOND, "Settings_col1");
		
		VerticalPanel contentPanel = new VerticalPanel();
		contentPanel.setWidth("100%");
		contentPanel.setStylePrimaryName("Settings_pContent");
		
		contentPanel.add(title);
		contentPanel.add(flexTable);
		
		this.initWidget(contentPanel);
		this.setWidth("100%");
	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	private Widget getColSecondLanguage() {
		HorizontalPanel p = new HorizontalPanel();
		p.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		p.setSpacing(5);
		
		p.add(new Label(constants.SettingsLabelLanguageR0()));
		
		ListBox languages = new ListBox();
		languages.addItem("English", "en");
		languages.addItem("Русский", "ru");
		
		p.add(languages);
		
		return p;
	}
	
	private Widget getColSecondPageSize() {
		HorizontalPanel p = new HorizontalPanel();
		p.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		p.setSpacing(5);
		
		p.add(new Label(constants.SettingsLabelPageSizeR0()));
		
		ListBox pageSize = new ListBox();
		pageSize.addItem("25");
		pageSize.addItem("50");
		pageSize.addItem("100");
		
		p.add(pageSize);
		
		p.add(new Label(constants.SettingsLabelPageSizeR1()));
		return p;
	}
}
